package com.EE7th.controller;

import com.EE7th.Result.PageResult;
import com.EE7th.Result.Result;
import com.EE7th.domain.Teacher;
import com.EE7th.dto.PageQueryDTO;
import com.EE7th.dto.TeacherDTO;
import com.EE7th.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    // 新增老师
    @PostMapping("/add")
    public Result<Void> addTeacher(@RequestBody Teacher teacher) {
        log.info("新增老师: {}", teacher);
        System.out.println("新增老师: " + teacher);
        teacherService.insertTeacher(teacher);
        return Result.success();
    }

    // 删除老师
    @DeleteMapping("/delete/{id}")
    public Result<Void> deleteTeacher(@PathVariable int id) {
        log.info("删除老师，ID: {}", id);
        teacherService.deleteTeacher(id);
        return Result.success();
    }

    // 老师关联班级
    @PostMapping("/linkClass")
    public Result<Void> linkClass(
            @RequestParam("teacherId") int teacherId,
            @RequestParam("classId") int classId) {
        log.info("老师关联班级，老师ID: {}, 班级ID: {}", teacherId, classId);
        teacherService.linkTeacherClass(teacherId, classId);
        return Result.success();
    }

    // 老师取消关联班级
    @DeleteMapping("/unlinkClass")
    public Result<Void> unlinkClass(
            @RequestParam("teacherId") int teacherId,
            @RequestParam("classId") int classId) {
        log.info("老师取消关联班级，老师ID: {}, 班级ID: {}", teacherId, classId);
        teacherService.unlinkTeacherClass(teacherId, classId);
        return Result.success();
    }

    // 获取老师选项
    @GetMapping("/select")
    public Result<List<TeacherDTO>> getTeacherSelection() {
        return Result.success(teacherService.getTeacherSelection());
    }

    // 查询老师列表
    @GetMapping("/page")
    public Result<PageResult> pageQuery(PageQueryDTO pageQueryDTO) {
        log.info("page query: {}", pageQueryDTO);
        PageResult pageResult = teacherService.pageQuery(pageQueryDTO);
        return Result.success(pageResult);
    }
}